package com.example.love_margin.Service.Impl.Until;

import com.example.love_margin.Entity.Until.Img;
import com.example.love_margin.Mapper.Until.ImgMapper;
import com.example.love_margin.Service.Until.ImgService;
import com.example.love_margin.Untils.FileNameUtils;
import com.example.love_margin.Untils.FileUtils;
import com.example.love_margin.Untils.ResultCode;
import com.example.love_margin.Untils.DeleteFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ImgServiceImpl implements ImgService {

    @Autowired
    ImgMapper imgMapper;

    //全局变量，方便更改图片绝对存储目录
    @Value("${web.imgURL}")
    private String imgURL;
    //全局变量，方便更改图片相对存储目录
    @Value("${web.imgURL1}")
    private String imgURL1;

    //全局变量，方便用来存储配置里的域名或者服务器ip
    @Value("${web.localUrl}")
    private String localUrl;


    @Override
    public Object addImg(MultipartFile file,String log) {
        Img img = new Img();
           String msg = "";
           String AllUrl = "/"+log;
           String AllUrl2 = log;
        //获取上传图片的文件名不带 / 的路径（非完全，后期查询接口对查询数据再拼接）存进对象t的Url的属性内
            String fileName = FileNameUtils.getFileName(file.getOriginalFilename());
            String admin = AllUrl2+"/"+fileName;
            String returnUrl = localUrl+admin;
            String path = imgURL+"/"+log;
            if (FileUtils.upload(file, path,fileName)){
                try {
                    img.setUrl(admin);
                    img.setState("unsuccessful");
                    imgMapper.addImg(img);
                    return ResultCode.SUCCESS.result(img);
                }catch (Exception e){
                    if(imgMapper.selectUrl(admin)!=1){
                        String KK = imgURL + AllUrl+"/" + fileName;
                        File dest = new File(KK);
                        if (fileName != null) {
                            if(dest.delete()){
                                return "删除成功";
                            }else {
                                return "文件不存在";
                            }

                        } else {
                            System.out.println("sb");
                            return "文件失效";
                        }
                    }else {
                        return "文件已上传";
                    }
                }



            }else {
                msg = "添加失败";
                return ResultCode.SUCCESS.result(msg);
            }
    }

    @Override
    public Object addImgs(MultipartFile[] files,String log) {
           List<Img> imgs = new ArrayList<>();
           StringBuffer fileNames = new StringBuffer();
            for (int i=0; i < files.length ; i++) {
                Img img = new Img();
                //上传文件
                String fileName = FileNameUtils.getFileName(files[i].getOriginalFilename());
                String kk = log +"/"+fileName;
                fileNames.append(kk);
                if(i < files.length-1){
                    fileNames.append(",");
                }
                FileUtils.upload(files[i], imgURL+"/"+ log,fileName);
                img.setUrl(kk);
                img.setState("unsuccessful");
                imgMapper.addImg(img);
                imgs.add(i,img);
                System.out.println(imgs);
            }
            return ResultCode.SUCCESS.result(imgs);

    }

    @Override
    public Object deleteTest(String url,String url2) {

        String ll = "D:/2/wamp/wamp/www/wwwroot/htdocs/cms/css/";
        DeleteFile deleteFile = new DeleteFile();
        if( deleteFile.moveFile(imgURL+"/"+url,url2)){
            System.out.println("移动成功");
        }
        return true;
//        String KK = imgURL+"/"+url;
//        File dest = new File(KK);
//        if (url!=null){
//            dest.delete();
//            return "删除成功";
//        }else {
//            System.out.println("sb");
//            return "删除失败";
//        }

    }

}
